package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.WechatService;
import nonapi.io.github.classgraph.json.JSONUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.xml.bind.annotation.XmlType;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Objects;

@Service
public class WechatServiceImpl implements WechatService {
    // 登录
    private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";

    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    // 获取手机号
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";


    @Value("${wechat.appId}")
    private String appId;

    @Value("${wechat.appSecret}")
    private String appSecret;

    /**
     * 获取微信openId
     *
     * @param code
     * @return
     */
    @Override
    public String getOpenId(String code) {
        //发送微信请求 请求参数code appid appsecret 还有个固定参数
        HashMap<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("appid", appId);
        paramsMap.put("secret", appSecret);
        paramsMap.put("js_code", code);

        //使用hutool工具向微信发送请求
        String result = HttpUtil.get(REQUEST_URL, paramsMap);
        JSONObject jsonObject = JSONUtil.parseObj(result);

        if (ObjectUtil.isNotEmpty(jsonObject.getInt("errcode"))) {
            throw new BaseException(jsonObject.getStr("errmsg"));
        }

        //解析结果从openId中返回
        return jsonObject.getStr("openid");
    }


    /**
     * 获取手机号码
     *
     * @return
     */
    @Override
    public String getPhone(String code) {
        //首先创建一个Post请求
        //准备好请求所需参数code 和 access_token(token接在url中)
        HashMap<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("code", code);

        //这里根据官方响应数据进行参考
        String result = HttpUtil.post(PHONE_REQUEST_URL + getToken(), JSONUtil.toJsonStr(paramsMap));
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if (!ObjectUtils.equals(jsonObject.getInt("errcode"),0)) {
            throw new BaseException(jsonObject.getStr("errmsg"));
        }


        return jsonObject.getJSONObject("phone_info").getStr("phoneNumber");
    }

    //请求实例 :GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET


    /**
     * 获取AccessToken
     *
     * @return
     */
    private String getToken() {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appId);
        paramMap.put("secret", appSecret);
        String result = HttpUtil.get(TOKEN_URL, paramMap);
        JSONObject jsonObject = JSONUtil.parseObj(result);

        if (ObjectUtil.isNotEmpty(jsonObject.getInt("errcode"))) {
            throw new BaseException(jsonObject.getStr("errmsg"));
        }

        return jsonObject.getStr("access_token");
    }


}
